package com.wan.service.user;

import com.wan.dao.BaseDao;
import com.wan.dao.user.UserDao;
import com.wan.dao.user.UserDaoImpl;
import com.wan.pojo.User;
import org.junit.Test;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class UserServiceImpl implements UserService{

    private UserDaoImpl userDaoImpl;
    public  UserServiceImpl() {
        userDaoImpl = new UserDaoImpl();
    }

    /**
     * 业务层会调用dao层，我们需要引入Dao层；
     * @param userCode
     * @param password
     * @return
     */
    @Override
    public User login(String userCode, String password) {
        Connection connection = null;
        User user = null;

        try{
            connection = BaseDao.getConnection();
            // 通过业务层调用dao层
            user = userDaoImpl.getLoginUser(connection,userCode);
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return user;
    }

    @Override
    public boolean updatePwd(int id, String pwd) {
//        System.out.println("service层的id是："+id);
//        System.out.println(pwd);
        Connection connection = null;
        boolean flag = false;

        try{
            connection = BaseDao.getConnection();
            // 通过业务层调用dao层
            int i  = userDaoImpl.updatePwd(connection,id,pwd);
            if(i > 0) {
                flag = true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(connection,null,null);
        }
        return flag;
    }

    @Override
    public int getUserCount(String userName, int userRole) {

        Connection conn = null;
        int userCount = 0;

        try {
            conn = BaseDao.getConnection();
            userCount = userDaoImpl.getUserCount(conn, userName, userRole);
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }


        return userCount;
    }

    @Override
    public List<User> getUserList(String userName, int userRole, int currentPageNo, int pageSize) {

        Connection conn = null;
        List<User> usersList = new ArrayList<User>();

        try {
            conn = BaseDao.getConnection();
            usersList = userDaoImpl.getUserList(conn, userName, userRole,currentPageNo,pageSize);
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }


        return usersList;
    }

    @Override
    public int addUser(User user) {
        // 业务层调用dao持久层
        Connection conn = null;
        int impactCount = 0;

        try {
            conn = BaseDao.getConnection();
            impactCount = userDaoImpl.addUser(conn,user);
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return impactCount;
    }

    @Override
    public int userExist(String userCode) {
        Connection conn = null;
        int userCount = 0;
        try {
            conn = BaseDao.getConnection();
            userCount = userDaoImpl.userExist(conn, userCode);
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return userCount;
    }

    @Override
    public int deleteUserById(int id) {
        Connection conn = null;
        int userCount = 0;
        try {
            conn = BaseDao.getConnection();
            userCount = userDaoImpl.deleteUserById(conn, id);
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return userCount;
    }

    @Override
    public int updateUser(User user,int modifyBy) {
        Connection conn = null;
        int userCount = 0;
        try {
            conn = BaseDao.getConnection();
            userCount = userDaoImpl.updateUser(conn, user,modifyBy);
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return userCount;
    }

    @Override
    public User getUserById(int id) {
        Connection conn = null;
        User user = null;
        int userCount = 0;
        try {
            conn = BaseDao.getConnection();
            user = userDaoImpl.getUserById(conn, id);
        }catch(Exception e) {
            e.printStackTrace();
        }finally {
            BaseDao.closeResource(conn,null,null);
        }
        return user;
    }
}
